package com.dc.bobaoge.project.v3.to.v2.business.core;

import com.dc.bobaoge.base.entity.message.common.po.MessageComment;
import com.dc.bobaoge.base.mapper.message.MessageCommentMapper;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreMessageComment;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreMessageCommentMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreMessageMapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.component.common.exception.BusinessSimpleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * MessageCommentResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-13 09:53:57
 */
@Slf4j
@Component
public class MessageCommentResolveMethod implements ResolveMethod {

    @Resource
    private MessageCommentMapper messageCommentMapper;

    @Resource
    private OldCoreMessageCommentMapper oldCoreMessageCommentMapper;
    @Resource
    private OldCoreMessageMapper oldCoreMessageMapper;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        // 查询回复消息信息
        var comment = messageCommentMapper.selectById(trigger.getTargetId());
        if (comment == null) {
            throw new BusinessSimpleException("留言不存在: " + trigger.getTargetId());
        }
        // 查询是否已经评论过
        var oldCoreMessageComment = oldCoreMessageCommentMapper.selectById(trigger.getTargetId());
        if (oldCoreMessageComment == null) {
            add(trigger, comment);
            return;
        }
        buildCommon(oldCoreMessageComment, comment);
        oldCoreMessageCommentMapper.updateById(oldCoreMessageComment);
    }

    /**
     * 新增信息
     *
     * @param trigger 日志信息
     * @param comment 留言信息
     */
    private void add(SyncTrigger trigger, MessageComment comment) {
        var oldCoreMessageComment = buildCommon(new OldCoreMessageComment(), comment);
        oldCoreMessageCommentMapper.insert(oldCoreMessageComment);
    }

    /**
     * 设置公共的信息
     *
     * @param oldCoreMessageComment 旧的信息
     * @param comment               新的信息
     * @return 旧的信息
     */
    private OldCoreMessageComment buildCommon(OldCoreMessageComment oldCoreMessageComment, MessageComment comment) {
        var message = oldCoreMessageMapper.selectById(comment.getFkMessageId());
        if (message == null) {
            throw new BusinessSimpleException("消息不存在: " + comment.getFkMessageId());
        }
        // 设置信息内容
        var content = buildContent(comment.getContent(), comment.getFkUserId(), message.getMessageUserId());
        oldCoreMessageComment.setCommentId(comment.getId());
        oldCoreMessageComment.setCommentMsgId(comment.getFkMessageId());
        oldCoreMessageComment.setCommentContent(content);
        oldCoreMessageComment.setCommentUserId(comment.getFkUserId());
        oldCoreMessageComment.setCommentUserPhone(comment.getPhone());
        oldCoreMessageComment.setCommentDate(comment.getAuditTime() == null ? comment.getGmtCreate() : comment.getAuditTime());
        if (comment.getIsDeleted() == 0) {
            oldCoreMessageComment.setCommentIsDelete(comment.getAuditStatus() - 1);
        } else {
            oldCoreMessageComment.setCommentIsDelete(1);
        }
        return oldCoreMessageComment;
    }

    /**
     * 调整创建的内容
     *
     * @param content           内容
     * @param sendUserId        发送用户id
     * @param messageSendUserId 消息发送用户id
     * @return 调整后的内容
     */
    private String buildContent(String content, Long sendUserId, Long messageSendUserId) {
        if (sendUserId != null && messageSendUserId != null) {
            if (sendUserId.equals(messageSendUserId)) {
                return "[楼主评论] " + content;
            }
        }
        return "[不是楼主评论] " + content;
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "message_comment";
    }

}
